<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix header-box">
      <span>文章分类</span>
      <el-button type="primary" size="mini" @click="isShow = true"
        >添加分类</el-button
      >
    </div>
    <!-- 分类列表 -->
    <!-- type="index"自动生成序号从1开始 -->
    <el-table :data="cateList">
      <el-table-column label="序号" type="index" width="100"></el-table-column>
      <el-table-column label="分类名称" prop="cate_name"></el-table-column>
      <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button type="primary" size="mini" @click="editCate(row.id)"
            >修改</el-button
          >
          <el-button type="danger" size="mini" @click="delCate(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="addForm.id ? '修改分类' : '添加分类'"
      :visible.sync="isShow"
      width="45%"
      @close="close"
    >
      <span>
        <!-- 添加的表单 -->
        <el-form
          :model="addForm"
          :rules="addRules"
          ref="addRef"
          label-width="85px"
        >
          <el-form-item label="分类名称" prop="cate_name">
            <el-input
              v-model="addForm.cate_name"
              minlength="1"
              maxlength="10"
            ></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input
              v-model="addForm.cate_alias"
              minlength="1"
              maxlength="15"
            ></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="addCate">{{
          addForm.id ? '修改' : '添加'
        }}</el-button>
      </span>
    </el-dialog>
  </el-card>

  <!--  -->
</template>

<script>
import { getCateApi, addCateApi, editCateApi, changeCateApi, delCateApi } from '@/api/article'
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [],
      isShow: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }

    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    async getCateList () {
      const { data } = await getCateApi()
      console.log('文章分类', data.data)
      this.cateList = data.data
    },
    close () {
      this.$refs.addRef.resetFields()
      this.addForm = {
        cate_name: '',
        cate_alias: ''
      }
    },

    addCate () {
      /**
       * 1. 兜底检验
       * 2. 发送请求，新增分类
       * 3. 刷新下分类列表=》才能看到最新的分类数据
       */
      this.$refs.addRef.validate(async (isok) => {
        if (isok) {
          if (!this.addForm.id) {
            await addCateApi(this.addForm)
          } else {
            await changeCateApi(this.addForm)
          }

          // this.$message.success(`${this.addForm.id}?'修改成功':'修改成功'`)
          this.$message.success(`${!this.addForm.id ? '添加' : '修改'}成功`)
          // 刷新下分类列表=》才能看到最新的分类数据
          this.getCateList()
          // 关闭弹层
          this.isShow = false
        }
      })
    },
    async editCate (id) {
      const { data } = await editCateApi(id)
      console.log('点击的当前行的数据', data.data)
      this.addForm = data.data
      this.isShow = true
    },
    // 删除
    delCate (row) {
      this.$confirm(`确认删除${row.cate_name}`, '提示').then(async () => {
        //  点击确定
        const { data } = await delCateApi(row.id)
        this.$message.success(data.message)
        this.getCateList()
      }).catch(() => {
        // 点击取消
      })
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
